//
//  LoginViewController.swift
//  ModernFoodWebsite
//
//  Created by apple on 16/5/29.
//  Copyright © 2016年 Lonnie. All rights reserved.
//

import UIKit
import CoreData
class LoginViewController: UIViewController {

    @IBOutlet weak var phoneTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        phoneTextField.configureToCommonStyle()
        passwordTextField.configureToCommonStyle()
        phoneTextField.addLeftView("auth_account_icon")
        passwordTextField.addLeftView("auth_password_icon")
    }

    @IBAction func login(sender: AnyObject) {
        let phone = phoneTextField.text
        let password = passwordTextField.text
        let req = NSFetchRequest(entityName: "Account")
       
        req.predicate = NSPredicate(format: "phone == %@ AND password == %@", phone!,password!)
        do {
         let datas = try DataController.shareInstance.managedObjectContext.executeFetchRequest(req)
            print("datas:\(datas)")
            if datas.count >= 1 {
                _ = UIAlertController(message: "登录成功", context: self)
                Account.setCurrentAccountKey(phone!)
                let app = UIApplication.sharedApplication().delegate as! AppDelegate
                app.switchRootViewController()
            } else {
                _ = UIAlertController(message: "登录失败", context: self)
            }
        } catch _ {
            
        }
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)
    }
}
